layui.use(['form','wangEditor'], function () {
    let form = layui.form,
        layer = layui.layer,
        wangEditor = layui.wangEditor,
        $ = layui.$;



    //富文本
    let editor = new wangEditor('#LAY_demo_editor');
    editor.customConfig.uploadImgServer = "/admin/Apply/Doupload";
    editor.customConfig.uploadFileName = 'image';
    editor.customConfig.pasteFilterStyle = false;
    editor.customConfig.uploadImgMaxLength = 5;
    editor.customConfig.uploadImgParams = {
        'mark': 'img',
        'field': 'image',
        'oldSrc': ''

    }
    editor.customConfig.uploadImgHooks = {
        success: function (xhr, editor, result) {
            // 图片上传并返回结果，图片插入成功之后触发
            // xhr 是 XMLHttpRequst 对象，editor 是编辑器对象，result 是服务器端返回的结果
        },
        fail: function (xhr, editor, result) {
            layer.msg('上传图片失败')
        },
        error: function (xhr, editor) {
            layer.msg('上传图片出错')

        },
        timeout: function (xhr, editor) {
            layer.msg('上传图片超时')
        },

        customInsert: function (insertImg, result, editor) {
            let url = "/upload/"+result.src
            insertImg(url)
            let urls=$("#editorimgurlHidden").val()+','+url
            $("#editorimgurlHidden").val(urls)

        }
    }


    editor.create();
    //初始化编辑器数据
    let info4=$("#hideDetail").html()
    let info5=$('<div>').html(info4).text()
    editor.txt.html(info5)



    //监听提交
    form.on('submit(saveBtn)', function (data) {

        let  postdata =data.field
        postdata.content=editor.txt.html()


        //发异步，把数据提交给php
        $.ajax({
            url: '/admin/Apply/ToSaveAgr?action=ajax',
            type: 'post',
            dataType: 'json',
            data:postdata,
            success: function(res) {
                console.log( res)

                if (res.status == true) {
                    layer.msg('保存成功', {icon: 1, time: 1000},
                        function(){
                            parent.location.reload();
                        })

                }
            },
            error: function(re) {

                layer.open({
                    title: '错误信息',
                    content: '<span style="color:red">'+re.responseJSON.errorCode+':</span>'+re.responseJSON.msg
                })
            }
        });




    });

});




